<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

use App\Lable;

class LableController extends Controller
{
    //定义查询数组
    protected static $field = [
        ['id'=>1,'searchtype'=>'编号'],
        ['id'=>2,'searchtype'=>'创建时间'],
        ['id'=>3,'searchtype'=>'更新时间'],
        ['id'=>4,'searchtype'=>'标签名字'],
    ];

    //标签列表载入
    public function index() {
        $search = input::get('searchtype');
        $param = input::get('param');
        $obj = new Lable;
        if($search == 1) {
            $obj = $obj->where('id','like',"%$param%");
        }elseif($search == 2) {
            $obj = $obj->where('create_time','like',"%$param%");
        }elseif($search == 3) {
            $obj = $obj->where('update_time','like',"%$param%");
        }elseif($search == 4) {
            $obj = $obj->where('name','like',"%$param%");
        }

        $list = $obj->all()->toArray();
        if($list == false && !empty($search)) {
            return  redirect()->back()->withInput()->withErrors('未查询到结果！');
        }
        $searchtype = self::$field;
        return view('admin.lable.index',['list'=>$list,'searchtype'=>$searchtype]);
    }

    //获取要更改的标签
    public function getLable($id) {
        $list = Lable::where('id',$id)->first();
        return view('admin.lable.updateLable',['list'=>$list]);
    }

    //更改标签
    public function updateLable() {
        $id = input::get('id');
        $data['name'] = input::get('name');
        if(empty($data['name'])) {
            return redirect()->back()->withInput()->withErrors('缺少参数!');
        }
        $list = Lable::where('id',$id)->update($data);
        if($list) {
            return Redirect::to('admin/lable')->withErrors('修改成功!');
        }
        else {
            return redirect()->back()->withInput()->withErrors('修改失败!');
        }
    }
}
